package com.rnkrsoft.suyan.config.server.services.impl;

import com.rnkrsoft.framework.orm.Pagination;
import com.rnkrsoft.suyan.config.internal.enums.AuthorityStatusEnum;
import com.rnkrsoft.suyan.config.server.config.dao.*;
import com.rnkrsoft.suyan.config.server.config.bo.*;
import com.rnkrsoft.suyan.config.server.config.entity.*;
import com.rnkrsoft.suyan.config.server.services.authority.domains.*;
import com.rnkrsoft.suyan.config.server.services.authority.services.AuthorityService;
import com.rnkrsoft.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by woate on 2019/7/4.
 */
@Service
public class AuthorityServiceImpl implements AuthorityService {
    @Autowired
    AuthorityInfoDAO authorityInfoDAO;

    @Override
    public QueryAuthorityResponse query(QueryAuthorityRequest request) {
        QueryAuthorityResponse response = new QueryAuthorityResponse();
        Pagination<QueryAuthorityInfoBO> pagination = new Pagination<QueryAuthorityInfoBO>(request.getPagination().getPageSize(), request.getPagination().getPageNo());
        if (StringUtils.isNotBlank(request.getMachineId())) {
            pagination.getParams().put("machineId", request.getMachineId());
        }
        if (StringUtils.isNotBlank(request.getResourceType())) {
            pagination.getParams().put("resourceType", request.getResourceType());
        }
        if (StringUtils.isNotBlank(request.getUserId())) {
            pagination.getParams().put("userId", request.getUserId());
        }
        if (StringUtils.isNotBlank(request.getCreateOperatorId())) {
            pagination.getParams().put("createOperatorId", request.getCreateOperatorId());
        }
        authorityInfoDAO.queryAuthority(pagination);
        response.getPagination().setPageSize(request.getPagination().getPageSize());
        response.getPagination().setPageNo(request.getPagination().getPageNo());
        response.getPagination().setTotal(pagination.getTotal());
        response.getPagination().setPageNum(pagination.getPageNum());
        for (QueryAuthorityInfoBO record : pagination.getRecords()) {
            response.addRecord(QueryAuthorityRecord.builder()
                            .authorityId(record.getAuthorityId())
                            .resourceId(record.getResourceId())
                            .resourceName(record.getResourceName())
                            .resourceType(record.getResourceType())
                            .groupId(record.getGroupId())
                            .artifactId(record.getArtifactId())
                            .version(record.getVersion())
                            .environmentName(record.getEnvironmentName())
                            .machineName(record.getMachineName())
                            .userName(record.getUserName())
                            .createOperatorName(record.getCreateOperatorName())
                            .build()
            );
        }
        return response;
    }

    @Override
    public FetchUserIdAndNameResponse fetchUserIdAndName(FetchUserIdAndNameRequest request) {
        FetchUserIdAndNameResponse response = new FetchUserIdAndNameResponse();
        return response;
    }

    @Override
    public FetchUserIdResponse fetchUserId(FetchUserIdRequest request) {
        FetchUserIdResponse response = new FetchUserIdResponse();
        return response;
    }

    @Override
    public DeleteAuthorityResponse deleteAuthority(DeleteAuthorityRequest request) {
        DeleteAuthorityResponse response = new DeleteAuthorityResponse();
        AuthorityInfoEntity authorityInfoEntity = AuthorityInfoEntity.builder()
                .authorityId(request.getAuthorityId())
                .deleteOperatorId(request.getLoginUserId())
                .deleteOperatorName(request.getLoginUserName())
                .authorityStatus(AuthorityStatusEnum.DELETE.getCode())
                .build();
        int updateCnt = authorityInfoDAO.updateByPrimaryKeySelective(authorityInfoEntity);
        return response;
    }
}
